<?php
    header('Access-Control-Allow-Origin: *');
	header("Access-Control-Allow-Method:POST,GET");
    header ( "Content-Type: text/html; charset=UTF-8" );
    require_once 'mysql.function.php';
	require_once 'response.class.php';
         
         $rawPostData = file_get_contents("php://input");
	     $postData = json_decode($rawPostData,true);
         $content = $postData["text_area"];
         $nickname = $postData["name"];
        //获取ip
        function getIp(){
		if (getenv("HTTP_CLIENT_IP"))
			$ip = getenv("HTTP_CLIENT_IP"); 
		else if(getenv("HTTP_X_FORWARDED_FOR"))
			$ip = getenv("HTTP_X_FORWARDED_FOR"); 
		else if(getenv("REMOTE_ADDR")) 
			$ip = getenv("REMOTE_ADDR"); 
		else
			$ip = time().rand(0,100);
		return $ip;
	}
	$ip = getIp();
    //echo $ip;
/*
//设置缓存
	$memcache = new Memcache();
	$memcache->connect('127.0.0.1', 11211) or die ("Could not connect");
	if( $memcache->get('confession_Publish_'.$ip) ){
		$message = array(
		    'content'=>'两次发表时间是1个小时哦~',
		);
		$re = new Response();
		$re->show(200,'fail',$message);
	}else{
		$memcache->set('confession_Publish_'.$ip,'TRUE',0,3600);
	}

	//判断数据
	if(empty($content)){
		$message = array(
			'content'=>'提交的内容不能为空~',		
		);
		$re = new Response();
		$re->show(200,'fail',$message);
	}
	if(mb_strlen($content,"utf-8")>300){
		$message = array(
		    'content'=>'提交的文字超过范围~',        
		);
		$re = new Response();
		$re->show(200,'fail',$message);
	}
*/
	if (empty($content)) {
        $message = array(
        	'content' => '提交的内容不能为空',
        	);
        $re = new Response();
		$re->show(200,'fail',$message);
       }

       $sqlhelper = new sqlhelper();
       $array = array('content'=>$content,'ip'=>$ip,'nickname'=>$nickname);
	   $table = "TreeHole";
	   $affectedRows = $sqlhelper->insert($table,$array);
	   $sqlhelper->close_connect();

	   $re = new Response();
	if($affectedRows > 0){
		$message = array(
			'content'=>'提交数据成功',
		);
		$re->show(200,'success',$message);
	}else{
		$message = array(
			'content'=>'提交数据失败,请@留言,联系公众号~',
		);
		$re->show(200,'fail',$message);
	}
	exit();
?>